#DNA export
#one-mode
#organizations via concepts
#congruence
#average activity
#ignore per document

#workspace
import graph_tool.all as gt #2.54
import matplotlib as mp #3.6.0
import igraph as ig #0.10.3
import pandas as pd #1.5.2
import numpy as np #1.24.3

#data
d = pd.read_csv("/m/triton/scratch/work/malkama5/politicisationScience/detwork.txt", index_col = 0, header = 0, sep = ";")
g = ig.Graph.Weighted_Adjacency(d.values, mode = "upper")
g.vs["name"] = d.index

#descriptives
print(g.vcount())
print(g.ecount())
print(g.density()) #g.ecount() / ((g.vcount() * (g.vcount() - 1)) / 2)
print(np.mean(g.es["weight"]))
print(np.std(g.es["weight"]))

#graph
g = g.to_graph_tool(vertex_attributes = {"name": "string"}, edge_attributes = {"weight": "float"})

#sbm
sd = 1; gt.seed_rng(sd); np.random.seed(sd)
s = gt.minimize_blockmodel_dl(g, state = gt.BlockState, state_args = dict(deg_corr = True, recs = [g.ep.weight], rec_types = ["real-exponential"]), multilevel_mcmc_args = dict(niter = 10, B_min = 1, B_max = 1))
print(s.entropy())

sd = 1; gt.seed_rng(sd); np.random.seed(sd)
s = gt.minimize_blockmodel_dl(g, state = gt.BlockState, state_args = dict(deg_corr = True, recs = [g.ep.weight], rec_types = ["real-exponential"]), multilevel_mcmc_args = dict(niter = 10, B_min = 2, B_max = 2))
print(s.entropy())

sd = 1; gt.seed_rng(sd); np.random.seed(sd)
s = gt.minimize_blockmodel_dl(g, state = gt.BlockState, state_args = dict(deg_corr = True, recs = [g.ep.weight], rec_types = ["real-exponential"]), multilevel_mcmc_args = dict(niter = 10, B_min = 1, B_max = 3))
print(s.entropy()) #choose lowest entropy model

#coordinates
sd = 1; gt.seed_rng(sd); np.random.seed(sd)
p = gt.sfdp_layout(g, groups = s.b, max_iter = 0, kappa = 5, gamma = 0.1)
s.draw(pos = p, vertex_text = g.vp.name, vertex_font_size = 4)

#fix
for v in g.vertices():
    if g.vp.name[v] == "Boreal Bioref":
        p[v][0] = p[v][0] - 0.5
        p[v][1] = p[v][1] + 1.5
    if g.vp.name[v] == "Finland Chamber of Commerce":
        p[v][0] = p[v][0] + 0.2
        p[v][1] = p[v][1] + 0.0
    if g.vp.name[v] == "Finnish Wind Power Association":
        p[v][0] = p[v][0] + 0.5
        p[v][1] = p[v][1] - 0.2
s.draw(pos = p)

#names
n = g.new_vp("string")
for v in g.vertices():
    if g.vp.name[v] == "ALDE EP":
        n[v] = "ALDE [EP]"
    if g.vp.name[v] == "Aalto University":
        n[v] = "Aalto University"
    if g.vp.name[v] == "BioS":
        n[v] = "BIOS"
    if g.vp.name[v] == "Bioenergy Association of Finland":
        n[v] = "Bioenergy Association of Finland"
    if g.vp.name[v] == "Blue Party":
        n[v] = "Blue Reform Party"        
    if g.vp.name[v] == "Boreal Bioref":
        n[v] = ""
    if g.vp.name[v] == "Central Union for Agricultural producers and forest owners (MTK)":
        n[v] = "Central Union of Agricultural Producers and Forest Owners"
    if g.vp.name[v] == "Centre Party":
        n[v] = "Centre Party"
    if g.vp.name[v] == "Christian Democrats ":
        n[v] = "Christian Democrats"
    if g.vp.name[v] == "Civil Party":
        n[v] = ""
    if g.vp.name[v] == "Confederation of Finnish Industries":
        n[v] = "Confederation of Finnish Industries"
    if g.vp.name[v] == "Fennovoima":
        n[v] = ""
    if g.vp.name[v] == "Fern":
        n[v] = "Fern"
    if g.vp.name[v] == "Finland Chamber of Commerce":
        n[v] = "Finnish Chambers of Commerce"
    if g.vp.name[v] == "FinnPulp":
        n[v] = "FinnPulp"
    if g.vp.name[v] == "Finnish Association for Nature Conservation":
        n[v] = "Finnish Association for Nature Conservation"
    if g.vp.name[v] == "Finnish Climate Change Panel":
        n[v] = "Finnish Climate Change Panel"
    if g.vp.name[v] == "Finnish Energy":
        n[v] = "Finnish Energy"
    if g.vp.name[v] == "Finnish Forest Association":
        n[v] = "Finnish Forest Association"
    if g.vp.name[v] == "Finnish Forest Centre":
        n[v] = "Finnish Forest Centre"
    if g.vp.name[v] == "Finnish Forest Industries":
        n[v] = "Finnish Forest Industries"
    if g.vp.name[v] == "Finnish Government":
        n[v] = "Finnish Government"
    if g.vp.name[v] == "Finnish Museum of Natural History":
        n[v] = "Finnish Museum of Natural History"
    if g.vp.name[v] == "Finnish Nature Panel":
        n[v] = "Finnish Nature Panel"
    if g.vp.name[v] == "Finnish Paper Workers Union":
        n[v] = "Finnish Paper Workers Union"
    if g.vp.name[v] == "Finnish Wildlife Agency":
        n[v] = "Finnish Wildlife Agency"    
    if g.vp.name[v] == "Finnish Wind Power Association":
        n[v] = ""
    if g.vp.name[v] == "Finns Party":
        n[v] = "Finns Party"   
    if g.vp.name[v] == "Greenpeace Finland":
        n[v] = "Greenpeace Finland"
    if g.vp.name[v] == "Greenpeace Sweden":
        n[v] = "Greenpeace Sweden"
    if g.vp.name[v] == "Kaidi":
        n[v] = "Kaidi"    
    if g.vp.name[v] == "Kone":
        n[v] = "Kone"
    if g.vp.name[v] == "LUKE":
        n[v] = "Natural Resource Institute Finland"    
    if g.vp.name[v] == "Left Alliance":
        n[v] = "Left Alliance"
    if g.vp.name[v] == "Metsä Group":
        n[v] = "Metsä Group"
    if g.vp.name[v] == "Ministry of Agriculture and Forestry":
        n[v] = "Ministry of Agriculture and Forestry"
    if g.vp.name[v] == "Ministry of Economic Affairs and Employment":
        n[v] = ""
    if g.vp.name[v] == "Ministry of transport and communications":
        n[v] = ""
    if g.vp.name[v] == "Movement Now":
        n[v] = ""
    if g.vp.name[v] == "National Coalition Party":
        n[v] = "National Coalition Party"    
    if g.vp.name[v] == "Natural Building Company":
        n[v] = ""    
    if g.vp.name[v] == "Neste":
        n[v] = ""
    if g.vp.name[v] == "SYKE":
        n[v] = "Finnish Environment Institute"
    if g.vp.name[v] == "Saami Council":
        n[v] = "Saami Council"    
    if g.vp.name[v] == "Sitra":
        n[v] = "Sitra"
    if g.vp.name[v] == "Social Democrats":
        n[v] = "Social Democratic Party"    
    if g.vp.name[v] == "Tampere Botanical Association":
        n[v] = "Tampere Botanical Association"
    if g.vp.name[v] == "Tampere University of Applied Sciences":
        n[v] = ""    
    if g.vp.name[v] == "The Greens":
        n[v] = "The Greens"
    if g.vp.name[v] == "The Greens EP":
        n[v] = "The Greens/European Free Alliance [EP]"    
    if g.vp.name[v] == "The Left EP":
        n[v] = ""    
    if g.vp.name[v] == "UPM":
        n[v] = "UPM"
    if g.vp.name[v] == "University of Eastern Finland":
        n[v] = "University of Eastern Finland"
    if g.vp.name[v] == "University of Helsinki":
        n[v] = "University of Helsinki"
    if g.vp.name[v] == "University of Jyväskylä":
        n[v] = "University of Jyväskylä"
    if g.vp.name[v] == "University of Turku":
        n[v] = "University of Turku"
    if g.vp.name[v] == "VTT":
        n[v] = "VTT Technical Research Centre of Finland"    
    if g.vp.name[v] == "Vihreä Lanka":
        n[v] = "Vihreä Lanka"
    if g.vp.name[v] == "WWF Finland":
        n[v] = "WWF Finland"    
    if g.vp.name[v] == "ePP Group":
        n[v] = "European People's Party [EP]"
    if g.vp.name[v] == "eu Comission":
        n[v] = "EU Commission"
    if g.vp.name[v] == "european Conservatives and Reformists (EP)":
        n[v] = "European Conservatives and Reformists [EP]"
    if g.vp.name[v] == "european Forest Institute":
        n[v] = "European Forest Institute"

#colours
cmap = mp.colors.ListedColormap(["#0F9D58", "#F4B400", "#4285F4"])

#visualise
s.draw(pos = p, vertex_size = 2, vertex_color = "#00000000", vcmap = cmap, vertex_text = n, vertex_font_size = 8.5, vertex_text_out_color = "#ffffff", vertex_text_color = "#000000", vertex_text_rotation = 6.0, vertex_text_position = 0, vertex_text_offset = [0.050, 0.015], vertex_aspect = 6, edge_pen_width = 0.4, vertex_text_out_width = 0.00, output_size = (1200, 1200), output = "/m/triton/scratch/work/malkama5/politicisationScience/doalition_sbm.svg") #replace path
np.savetxt("/m/triton/scratch/work/malkama5/politicisationScience/doalition_sbm.csv", pd.DataFrame({"vertex": g.vp.name, "block": s.b}).sort_values("block").values, delimiter = ";", fmt = "%s", comments = "") #replace path

#visualisation with edge bundling
ebundle = gt.NestedBlockState(g, bs = [s.b, s.b])
gt.draw_hierarchy(ebundle, vertex_color = "#00000000", vcmap = cmap, edge_pen_width = 0.4, vertex_size = 8, hvertex_size = 0, hedge_pen_width = 0, hedge_marker_size = 0, vertex_text_color = "#000000", vertex_text = n, vertex_text_position = "centered", output = "/m/triton/scratch/work/malkama5/politicisationScience/doalition_sbm_edge_bundle.svg") #replace path
